{% extends "base.html" %}
{% load crispy_forms_tags %}

{% block title %}关联性分析 - 在线跑批系统{% endblock %}

{% block extra_css %}
<!-- 引入CodeMirror用于PL/SQL语法高亮 -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.2/codemirror.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.2/theme/dracula.min.css">
{% endblock %}

{% block content %}
<h1><i class="fas fa-project-diagram me-2"></i>存储过程关联性分析</h1>

<div class="alert alert-info mt-3">
    <i class="fas fa-info-circle me-2"></i>
    该功能仅支持Oracle数据库，可分析存储过程涉及的所有表结构、索引、触发器、数据量，以及调用的所有自定义存储过程和函数。
</div>

<div class="row mt-4">
    <div class="col-md-12">
        <div class="card">
            <div class="card-body">
                <form method="post" novalidate>
                    {% csrf_token %}
                    
                    <div class="row">
                        <div class="col-md-12">
                            {{ form.db_connection|as_crispy_field }}
                        </div>
                    </div>
                    
                    <div class="row">
                        <div class="col-md-12">
                            <label class="form-label">{{ form.stored_procedure.label }}</label>
                            {{ form.stored_procedure }}
                            <small class="form-text text-muted">请输入完整的Oracle存储过程代码（包含CREATE OR REPLACE PROCEDURE语句）</small>
                        </div>
                    </div>
                    
                    <div class="mt-4">
                        <button type="submit" class="btn btn-primary">
                            <i class="fas fa-search-plus me-1"></i> 开始分析
                        </button>
                        <button type="button" class="btn btn-outline-secondary ms-2" id="clearCodeBtn">
                            <i class="fas fa-trash me-1"></i> 清空
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

{% if analysis_results %}
<div class="mt-5">
    <h3>
        <i class="fas fa-file-code me-2"></i>
        存储过程: {{ analysis_results.procedure_name }}
    </h3>
    
    <!-- 表结构信息 -->
    <div class="card mt-3">
        <div class="card-header bg-primary text-white">
            <h5 class="mb-0">
                <i class="fas fa-table me-2"></i>涉及的表和视图 ({{ analysis_results.tables|length }})
            </h5>
        </div>
        <div class="card-body">
            {% for table_name, table_info in analysis_results.tables.items %}
            <div class="mb-4 p-3 border rounded">
                <div class="d-flex justify-content-between align-items-center mb-2">
                    <h6 class="mb-0">
                        {{ table_name }} 
                        <span class="badge bg-secondary">{{ table_info.type }}</span>
                    </h6>
                    <button class="btn btn-sm btn-outline-primary toggleDetails" data-target="table-{{ table_name }}">
                        <i class="fas fa-chevron-down"></i> 详情
                    </button>
                </div>
                
                <div id="table-{{ table_name }}" class="details-content d-none mt-3">
                    <div class="row mb-3">
                        <div class="col-md-12">
                            <div class="small text-muted">数据量: {{ table_info.row_count }} 行</div>
                        </div>
                    </div>
                    
                    <!-- 表结构DDL -->
                    <div class="mb-3">
                        <h7 class="text-primary"><i class="fas fa-code me-1"></i>表结构 DDL:</h7>
                        <pre class="bg-light p-3 rounded mt-1 overflow-auto" style="max-height: 300px;"><code>{{ table_info.ddl }}</code></pre>
                    </div>
                    
                    <!-- 索引信息 -->
                    {% if table_info.indexes %}
                    <div class="mb-3">
                        <h7 class="text-primary"><i class="fas fa-search me-1"></i>索引 ({{ table_info.indexes|length }}):</h7>
                        <div class="mt-1">
                            {% for index_name, columns in table_info.indexes.items %}
                            <div class="mb-2">
                                <div class="font-weight-bold">{{ index_name }} <span class="small text-muted">({{ columns.0.type }})</span></div>
                                <div class="ms-3">
                                    {% for col in columns %}
                                    <div>{{ col.column }}</div>
                                    {% endfor %}
                                </div>
                            </div>
                            {% endfor %}
                        </div>
                    </div>
                    {% endif %}
                    
                    <!-- 触发器信息 -->
                    {% if table_info.triggers %}
                    <div class="mb-3">
                        <h7 class="text-primary"><i class="fas fa-exclamation-triangle me-1"></i>触发器 ({{ table_info.triggers|length }}):</h7>
                        {% for trigger in table_info.triggers %}
                        <div class="mb-3 p-2 bg-light rounded">
                            <div class="font-weight-bold">{{ trigger.name }}</div>
                            <div class="small"><span class="text-muted">类型:</span> {{ trigger.type }}</div>
                            <div class="small"><span class="text-muted">事件:</span> {{ trigger.event }}</div>
                            <div class="mt-1">
                                <button class="btn btn-sm btn-outline-secondary toggleTriggerDDL" data-target="trigger-{{ trigger.name }}">
                                    <i class="fas fa-chevron-down"></i> 显示DDL
                                </button>
                                <pre id="trigger-{{ trigger.name }}" class="bg-white p-2 rounded mt-1 overflow-auto d-none" style="max-height: 200px;"><code>{{ trigger.ddl }}</code></pre>
                            </div>
                        </div>
                        {% endfor %}
                    </div>
                    {% endif %}
                </div>
            </div>
            {% endfor %}
        </div>
    </div>
    
    <!-- 依赖的存储过程 -->
    {% if analysis_results.dependent_procedures %}
    <div class="card mt-4">
        <div class="card-header bg-success text-white">
            <h5 class="mb-0">
                <i class="fas fa-cogs me-2"></i>依赖的存储过程 ({{ analysis_results.dependent_procedures|length }})
            </h5>
        </div>
        <div class="card-body">
            {% for proc_name, proc_info in analysis_results.dependent_procedures.items %}
            <div class="mb-4 p-3 border rounded">
                <div class="d-flex justify-content-between align-items-center mb-2">
                    <h6 class="mb-0">{{ proc_name }}</h6>
                    <button class="btn btn-sm btn-outline-success toggleDetails" data-target="proc-{{ proc_name }}">
                        <i class="fas fa-chevron-down"></i> 详情
                    </button>
                </div>
                
                <div id="proc-{{ proc_name }}" class="details-content d-none mt-3">
                    <!-- 使用的表 -->
                    <div class="mb-3">
                        <h7 class="text-success"><i class="fas fa-table me-1"></i>使用的表 ({{ proc_info.tables|length }}):</h7>
                        <div class="mt-1">
                            {% for table in proc_info.tables %}
                            <span class="badge bg-light text-dark me-1">{{ table }}</span>
                            {% endfor %}
                        </div>
                    </div>
                    
                    <!-- 存储过程代码 -->
                    <div>
                        <h7 class="text-success"><i class="fas fa-code me-1"></i>存储过程代码:</h7>
                        <pre class="bg-light p-3 rounded mt-1 overflow-auto" style="max-height: 300px;"><code>{{ proc_info.code }}</code></pre>
                    </div>
                </div>
            </div>
            {% endfor %}
        </div>
    </div>
    {% endif %}
    
    <!-- 依赖的函数 -->
    {% if analysis_results.dependent_functions %}
    <div class="card mt-4">
        <div class="card-header bg-info text-white">
            <h5 class="mb-0">
                <i class="fas fa-calculator me-2"></i>依赖的函数 ({{ analysis_results.dependent_functions|length }})
            </h5>
        </div>
        <div class="card-body">
            {% for func_name, func_info in analysis_results.dependent_functions.items %}
            <div class="mb-4 p-3 border rounded">
                <div class="d-flex justify-content-between align-items-center mb-2">
                    <h6 class="mb-0">{{ func_name }}</h6>
                    <button class="btn btn-sm btn-outline-info toggleDetails" data-target="func-{{ func_name }}">
                        <i class="fas fa-chevron-down"></i> 详情
                    </button>
                </div>
                
                <div id="func-{{ func_name }}" class="details-content d-none mt-3">
                    <!-- 使用的表 -->
                    <div class="mb-3">
                        <h7 class="text-info"><i class="fas fa-table me-1"></i>使用的表 ({{ func_info.tables|length }}):</h7>
                        <div class="mt-1">
                            {% for table in func_info.tables %}
                            <span class="badge bg-light text-dark me-1">{{ table }}</span>
                            {% endfor %}
                        </div>
                    </div>
                    
                    <!-- 函数代码 -->
                    <div>
                        <h7 class="text-info"><i class="fas fa-code me-1"></i>函数代码:</h7>
                        <pre class="bg-light p-3 rounded mt-1 overflow-auto" style="max-height: 300px;"><code>{{ func_info.code }}</code></pre>
                    </div>
                </div>
            </div>
            {% endfor %}
        </div>
    </div>
    {% endif %}
</div>
{% endif %}

{% endblock %}

{% block extra_js %}
<!-- 引入CodeMirror及PL/SQL模式 -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.2/codemirror.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.2/mode/sql/sql.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.2/addon/hint/sql-hint.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.2/addon/hint/show-hint.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.2/addon/hint/show-hint.min.css">

<script>
    // 初始化PL/SQL编辑器
    const plsqlEditor = CodeMirror.fromTextArea(document.querySelector('textarea[name="stored_procedure"]'), {
        mode: 'text/x-sql',
        theme: 'dracula',
        lineNumbers: true,
        indentWithTabs: true,
        smartIndent: true,
        lineWrapping: true,
        matchBrackets: true,
        autoCloseBrackets: true,
        hintOptions: {
            tables: {
                // 可以在这里添加数据库对象信息，用于自动提示
            }
        }
    });
    
    // 清空按钮事件
    document.getElementById('clearCodeBtn').addEventListener('click', function() {
        plsqlEditor.setValue('');
    });
    
    // 详情切换按钮事件
    document.querySelectorAll('.toggleDetails').forEach(button => {
        button.addEventListener('click', function() {
            const targetId = this.getAttribute('data-target');
            const targetElement = document.getElementById(targetId);
            
            if (targetElement.classList.contains('d-none')) {
                targetElement.classList.remove('d-none');
                this.innerHTML = '<i class="fas fa-chevron-up"></i> 收起';
            } else {
                targetElement.classList.add('d-none');
                this.innerHTML = '<i class="fas fa-chevron-down"></i> 详情';
            }
        });
    });
    
    // 触发器DDL切换按钮事件
    document.querySelectorAll('.toggleTriggerDDL').forEach(button => {
        button.addEventListener('click', function() {
            const targetId = this.getAttribute('data-target');
            const targetElement = document.getElementById(targetId);
            
            if (targetElement.classList.contains('d-none')) {
                targetElement.classList.remove('d-none');
                this.innerHTML = '<i class="fas fa-chevron-up"></i> 隐藏DDL';
            } else {
                targetElement.classList.add('d-none');
                this.innerHTML = '<i class="fas fa-chevron-down"></i> 显示DDL';
            }
        });
    });
    
    // 按Ctrl+Enter执行分析
    plsqlEditor.setOption('extraKeys', {
        "Ctrl-Enter": function(instance) {
            document.querySelector('form').submit();
        },
        "Tab": function(instance) {
            instance.replaceSelection("  ", "end");
        }
    });
</script>
{% endblock %}
